/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306.table;

import my12306.http.TrainInfo;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Hashtable;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jack
 */
public class MyTable extends JPanel {

    HashMap<String, TrainInfo> rowData;
    String[] columnNames;
    public JTable table;

    public MyTable(HashMap<String, TrainInfo> data, String[] columns) {
        super(new GridLayout(1, 0));
        this.rowData = data;
        this.columnNames = columns;
        table = new JTable(new MyTableModel());
        table.setFillsViewportHeight(true);
        table.setAutoCreateRowSorter(true);
        table.setPreferredScrollableViewportSize(new Dimension(800, 360));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(new JScrollPane(table));
//        add(table);
    }

    class MyTableModel extends AbstractTableModel {

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rowData.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return (Object) rowData.get(rowData.keySet().toArray()[row]).toArray()[col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            return false;
        }
    }
}
